import time
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
class ElementsBase(): # todo：创建base类，封装基础api
    def __init__(self,driver):
        """
        :param driver:  初始化信息传入driver
        """
        self.driver = driver
        self.TouchAction = TouchAction(self.driver)
    def find_element(self,loc):
        """
        :param loc:  运用显示等待获取元素，让代码变得更健壮 。以元组的方式传入，loc = (Mobile.ID,"元素")
        :return:  返回元素
        """
        return WebDriverWait(self.driver,10,0.5).until(expected_conditions.presence_of_element_located(loc))

    def find_elements(self,loc):
        """
        :param loc:   运用显示等待获取元素，让代码变得更健壮 。以元组的方式传入，loc = (Mobile.ID,"元素")
        :return:  返回一组符合条件的元素
        """
        return WebDriverWait(self.driver,10,0.5).until(expected_conditions.presence_of_all_elements_located(loc))

    def click_element(self,loc):
        """
        :param loc:  点击元素。其中的loc以元组的方式传入（Mobile.XPATH,""）
        """
        self.find_element(loc).click()

    def click_elements(self,loc,num):
        """
        :param loc:  点击元素
        :param num:  要点击第几个
        :return:
        """
        self.find_elements(loc)[num].click()
    """
    def click_Textxpath(self,text):
        #:param text: 根据xpath点击定位
        from appium.webdriver.common.mobileby import MobileBy
        self.find_element((MobileBy.XPATH,"//*[@text()="+"'"+text+"']")).click()
    """
    def get_attribute(self, loc, key):
        """
        :param loc:  loc传入元组的方式
        :param key:  key传入想要获取元素的value值
        :return: 返回的是想要获取的元素的值
        """
        values = self.find_element(loc)
        return values.get_attribute(key)

    def touch_tap(self,x,y,duration = 100): # todo：轻点坐标
        """
        :param x:  x坐标值
        :param y:  y坐标值
        :param duration:   给的值决定了点击的速度
        """
        screen_width = self.driver.get_window_size()['width'] # todo：获取屏幕的宽
        screen_height = self.driver.get_window_size()['height'] # todo：获取屏幕的高
        a = (float(x)/screen_width) * screen_width
        x1 = int(a)
        b = (float(y)/screen_height) *screen_height
        y1 = int(b)
        self.driver.tap([(x1,y1),(x1,y1),duration])

    def Adaptation(self,x,y):
        """
        :param x: 待适配的x值
        :param y: 待适配的y值
        :return: 返回已经适配好的x值，y值
        """
        screen_width = self.driver.get_window_size()['width']  # todo：获取手机屏幕的宽
        screen_height = self.driver.get_window_size()['height']  # todo：获取手机屏幕的高
        a = (float(x) / int(screen_width)) * int(screen_width)
        x1 = int(a)
        b = (float(y) / int(screen_height)) * int(screen_height)
        y1 = int(b)
        return (x1,y1)
    def press_move_to(self,start_x,start_y,end_x,end_y):
        """
        :param start_x: 起始的x值
        :param start_y:  起始的y值
        :param end_x:  结束的x值
        :param end_y: 结束的y值
        """
        start_x_y = self.Adaptation(int(start_x),int(start_y))
        end_x_y = self.Adaptation(int(end_x),int(end_y))
        time.sleep(3)
        self.TouchAction.press(x = start_x_y[0],y = start_x_y[1]).move_to(x = end_x_y[0],y = end_x_y[1]).release().wait(3000).perform()



    def input_text(self,loc,text):
        """
        :param loc:  元素
        :param text:  待输入的文本信息
        """
        s = self.find_element(loc)
        s.clear() # todo：先清除
        s.send_keys(text) # todo：再输入

    def sweep_(self,start_x,start_y,end_x,end_y,durations=5000):
        """
        :param start_x: 起始的x值
        :param start_y: 起始的y值
        :param end_x: 要拖动到指定的x值
        :param end_y: 要拖动的指定的y值
        :param durations: 滑动这个操作⼀共持续的时间⻓度（也可以称惯性），单位：ms（毫秒）
        :return:
        """
        _x_y_ = self.Adaptation(start_x,start_y)
        end_x_y_ = self.Adaptation(end_x,end_y)
        self.driver.swipe(_x_y_[0],_x_y_[1],end_x_y_[0],end_x_y_[1],duration = durations)
        #self.driver.swipe(start_x,start_y,end_x,end_y,duration = durations)

    def TouchTap(self,x,y):
        """
        :param x: 待点击坐标的x值
        :param y: 待点击坐标的y值
        """
        _x_y_ = self.Adaptation(x,y)
        time.sleep(3)
        TouchAction(self.driver).tap(x = _x_y_[0],y = _x_y_[1]).perform()





